// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Squashfs - a compressed read only filesystem for Linux
 *
 * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008
 * Phillip Lougher <phillip@squashfs.org.uk>
 *
 * block.c
 */

/*
 * This file implements the low-level routines to read and decompress
 * datablocks and metadata blocks.
 */

#include <linux/blkdev.h>
#include <linux/fs.h>
#include <linux/vfs.h>
#include <linux/slab.h>
#include <linux/string.h>
#include <linux/buffer_head.h>
#include <linux/bio.h>
#ifdef CONFIG_RTOS_SQUASHFS_8M_BLOCK_SIZE
#include <linux/uio.h>
#endif

#include "squashfs_fs.h"
#include "squashfs_fs_sb.h"
#include "squashfs.h"
#include "decompressor.h"
#include "page_actor.h"

#ifdef CONFIG_RTOS_SQUASHFS_EXTRA_HEADER
#include "squashfs_extra_header.h"

char squashfs_rootfs_device[64];

void squashfs_rootfs_device_setup(char *root_name, int len)
{
	if (root_name[0] && (strncmp(root_name, "/dev/", 5) == 0) && (!squashfs_rootfs_device[0]))
		strlcpy(squashfs_rootfs_device, root_name+5, len-5);
}
/* NOTE: default extra_head_offset
 *		= sizeof(hw_swm_file_head_st) + sizeof(image_header_ttt)
 *		= 0x94
 * if hw_swm_file_head_st or image_header_ttt changed, the extra_head_offset
 * should be changed too.
 */
unsigned int extra_head_offset = sizeof(hw_swm_file_head_st) + sizeof(image_header_ttt);
#endif

/*
 * Returns the amount of bytes copied to the page actor.
 */
static int copy_bio_to_actor(struct bio *bio,
			     struct squashfs_page_actor *actor,
			     int offset, int req_length)
{
	void *actor_addr = squashfs_first_page(actor);
	struct bvec_iter_all iter_all = {};
	struct bio_vec *bvec = bvec_init_iter_all(&iter_all);
	int copied_bytes = 0;
	int actor_offset = 0;

	if (WARN_ON_ONCE(!bio_next_segment(bio, &iter_all)))
		return 0;

	while (copied_bytes < req_length) {
		int bytes_to_copy = min_t(int, bvec->bv_len - offset,
					  PAGE_SIZE - actor_offset);

		bytes_to_copy = min_t(int, bytes_to_copy,
				      req_length - copied_bytes);
		memcpy(actor_addr + actor_offset,
		       page_address(bvec->bv_page) + bvec->bv_offset + offset,
		       bytes_to_copy);

		actor_offset += bytes_to_copy;
		copied_bytes += bytes_to_copy;
		offset += bytes_to_copy;

		if (actor_offset >= PAGE_SIZE) {
			actor_addr = squashfs_next_page(actor);
			if (!actor_addr)
				break;
			actor_offset = 0;
		}
		if (offset >= bvec->bv_len) {
			if (!bio_next_segment(bio, &iter_all))
				break;
			offset = 0;
		}
	}
	squashfs_finish_page(actor);
	return copied_bytes;
}

#ifdef CONFIG_RTOS_SQUASHFS_8M_BLOCK_SIZE
static struct bio *squashfs_bio_kmalloc(gfp_t gfp_mask, unsigned int nr_iovecs)
{
	struct bio *bio;

	bio = kmalloc(struct_size(bio, bi_inline_vecs, nr_iovecs), gfp_mask);
	if (unlikely(!bio))
		return NULL;

	bio_init(bio, NULL, 0);

	bio->bi_max_vecs = nr_iovecs;
	bio->bi_io_vec = bio->bi_inline_vecs;
	return bio;
}

static int squashfs_largeblock_bio_read(struct super_block *sb, u64 index, int length,
			     struct bio **biop, int *block_offset)
{
	struct squashfs_sb_info *msblk = sb->s_fs_info;
	const u64 read_start = round_down(index, msblk->devblksize);
	sector_t block = read_start >> msblk->devblksize_log2;
	const u64 read_end = round_up(index + length, msblk->devblksize);
	const sector_t block_end = read_end >> msblk->devblksize_log2;
	int offset = read_start - round_down(index, PAGE_SIZE);
	int total_len = (block_end - block) << msblk->devblksize_log2;
	int page_count = DIV_ROUND_UP(total_len + offset, PAGE_SIZE);
	int error, i;
	struct bio_vec *bvec;
	struct bvec_iter_all iter_all;
	struct bio *bio, *bio_main;

	bio_main = squashfs_bio_kmalloc(GFP_NOIO, page_count);
	if (!bio_main)
		return -ENOMEM;

	bio_main->bi_opf = READ;
	bio_main->bi_iter.bi_sector = block * (msblk->devblksize >> SECTOR_SHIFT);

	while (page_count > 0) {
		int page_required = page_count > UIO_MAXIOV ? UIO_MAXIOV : page_count;

		bio = bio_kmalloc(GFP_NOIO, page_required);
		if (!bio)
			goto out_free_bio;

		bio_set_dev(bio, sb->s_bdev);
		bio->bi_opf = READ;
		bio->bi_iter.bi_sector = block * (msblk->devblksize >> SECTOR_SHIFT);

		for (i = 0; i < page_required; ++i) {
			unsigned int len =
				min_t(unsigned int, PAGE_SIZE - offset, total_len);
			struct page *page = alloc_page(GFP_NOIO);

			if (!page) {
				error = -ENOMEM;
				goto out_free_bio;
			}
			if (!bio_add_page(bio, page, len, offset)) {
				error = -EIO;
				__free_pages(page, 0);
				goto out_free_bio;
			}
			offset = 0;
			total_len -= len;
			block += len >> msblk->devblksize_log2;
		}

		page_count -= page_required;

		error = submit_bio_wait(bio);
		if (error)
			goto out_free_bio;

		bio_for_each_segment_all(bvec, bio, iter_all) {
			if (!bio_add_page(bio_main, bvec->bv_page, bvec->bv_len, bvec->bv_offset)) {
				error = -EIO;
				goto out_free_bio;
			}
		}

		bio_put(bio);
	}

	*biop = bio_main;
	*block_offset = index & ((1 << msblk->devblksize_log2) - 1);
	return 0;

out_free_bio:
	if (bio_main) {
		bio_free_pages(bio_main);
		bio_put(bio_main);
	}
	if (bio) {
		bio_free_pages(bio);
		bio_put(bio);
	}
	return error;
}
#endif

static int squashfs_bio_read(struct super_block *sb, u64 index, int length,
			     struct bio **biop, int *block_offset)
{
	struct squashfs_sb_info *msblk = sb->s_fs_info;
	const u64 read_start = round_down(index, msblk->devblksize);
	const sector_t block = read_start >> msblk->devblksize_log2;
	const u64 read_end = round_up(index + length, msblk->devblksize);
	const sector_t block_end = read_end >> msblk->devblksize_log2;
	int offset = read_start - round_down(index, PAGE_SIZE);
	int total_len = (block_end - block) << msblk->devblksize_log2;
	const int page_count = DIV_ROUND_UP(total_len + offset, PAGE_SIZE);
	int error, i;
	struct bio *bio;

#ifdef CONFIG_RTOS_SQUASHFS_8M_BLOCK_SIZE
	if (page_count > UIO_MAXIOV)
		return squashfs_largeblock_bio_read(sb, index, length, biop, block_offset);
#endif

	if (page_count <= BIO_MAX_PAGES)
		bio = bio_alloc(GFP_NOIO, page_count);
	else
		bio = bio_kmalloc(GFP_NOIO, page_count);

	if (!bio)
		return -ENOMEM;

	bio_set_dev(bio, sb->s_bdev);
	bio->bi_opf = READ;
	bio->bi_iter.bi_sector = block * (msblk->devblksize >> SECTOR_SHIFT);

	for (i = 0; i < page_count; ++i) {
		unsigned int len =
			min_t(unsigned int, PAGE_SIZE - offset, total_len);
		struct page *page = alloc_page(GFP_NOIO);

		if (!page) {
			error = -ENOMEM;
			goto out_free_bio;
		}
		if (!bio_add_page(bio, page, len, offset)) {
			error = -EIO;
			goto out_free_bio;
		}
		offset = 0;
		total_len -= len;
	}

	error = submit_bio_wait(bio);
	if (error)
		goto out_free_bio;

	*biop = bio;
	*block_offset = index & ((1 << msblk->devblksize_log2) - 1);
	return 0;

out_free_bio:
	bio_free_pages(bio);
	bio_put(bio);
	return error;
}

#ifdef CONFIG_RTOS_SQUASHFS_EXTRA_HEADER
static int squashfs_extra_header_size(struct super_block *sb, u64 index, int length, u64 *next_index,
				struct squashfs_page_actor *output)
{
	struct squashfs_sb_info *msblk = sb->s_fs_info;
	u64 tmp_next_index = 0;
	hw_swm_file_head_st swm_head_info;
	struct squashfs_page_actor tmp_output = *output;
	int extra_offset = 0;
	int ret;

	if (strncmp(squashfs_rootfs_device, sb->s_id, sizeof(squashfs_rootfs_device)) == 0) {
		extra_offset = extra_head_offset;
	} else {
		memset(&swm_head_info, 0, sizeof(hw_swm_file_head_st));
		if (index == 0 && msblk->is_extra_header == 0) {
			if (next_index != NULL)
				tmp_next_index = *next_index;
			msblk->is_extra_header = -1;
			/* read the squashfs head firstly, use tmp_next_index and tmp_output to avoid they are changed here */
			ret = squashfs_read_data(sb, index, length, &tmp_next_index, &tmp_output);
			if (ret < 0)
				return ret;
			memcpy(&swm_head_info, *tmp_output.page, sizeof(hw_swm_file_head_st));
			if (HW_SWM_FILE_HEAD_MAGIC == swm_head_info.magic) {
				/* has ont extra header, for boot from flash */
				msblk->is_extra_header = 1;
			} else {
				/* no ont extra header */
				msblk->is_extra_header = 2;
			}
		}
		/* flash boot need to jump the head  */
		if (msblk->is_extra_header == 1)
			extra_offset = sizeof(hw_swm_file_head_st) + sizeof(image_header_ttt);
	}

	return extra_offset;
}
#endif

/*
 * Read and decompress a metadata block or datablock.  Length is non-zero
 * if a datablock is being read (the size is stored elsewhere in the
 * filesystem), otherwise the length is obtained from the first two bytes of
 * the metadata block.  A bit in the length field indicates if the block
 * is stored uncompressed in the filesystem (usually because compression
 * generated a larger block - this does occasionally happen with compression
 * algorithms).
 */
int squashfs_read_data(struct super_block *sb, u64 index, int length,
		       u64 *next_index, struct squashfs_page_actor *output)
{
	struct squashfs_sb_info *msblk = sb->s_fs_info;
	struct bio *bio = NULL;
	int compressed;
	int res;
	int offset;
	int extra_offset = 0;
#ifdef CONFIG_RTOS_SQUASHFS_BCJ
	int bcj_index = BCJ_NONE;
#endif

#ifdef CONFIG_RTOS_SQUASHFS_EXTRA_HEADER
	extra_offset = squashfs_extra_header_size(sb, index, length, next_index, output);
#endif

	if (length) {
		/*
		 * Datablock.
		 */
		compressed = SQUASHFS_COMPRESSED_BLOCK(length);
#ifdef CONFIG_RTOS_SQUASHFS_BCJ
		bcj_index = SQUASHFS_GET_BCJ_INDEX(length);
		if (bcj_index >= BCJ_INVAL) {
			res = -EINVAL;
			ERROR("Unrecognize bcj type %d!\n", bcj_index);
			goto out;
		}
#endif
		length = SQUASHFS_COMPRESSED_SIZE_BLOCK(length);
		TRACE("Block @ 0x%llx, %scompressed size %d, src size %d\n",
			index, compressed ? "" : "un", length, output->length);
	} else {
		/*
		 * Metadata block.
		 */
		const u8 *data;
		struct bvec_iter_all iter_all = {};
		struct bio_vec *bvec = bvec_init_iter_all(&iter_all);

		if (index + 2 > msblk->bytes_used) {
			res = -EIO;
			goto out;
		}

		res = squashfs_bio_read(sb, index + extra_offset, 2, &bio, &offset);
		if (res)
			goto out;

		if (WARN_ON_ONCE(!bio_next_segment(bio, &iter_all))) {
			res = -EIO;
			goto out_free_bio;
		}
		/* Extract the length of the metadata block */
		data = page_address(bvec->bv_page) + bvec->bv_offset;
		length = data[offset];
		if (offset < bvec->bv_len - 1) {
			length |= data[offset + 1] << 8;
		} else {
			if (WARN_ON_ONCE(!bio_next_segment(bio, &iter_all))) {
				res = -EIO;
				goto out_free_bio;
			}
			data = page_address(bvec->bv_page) + bvec->bv_offset;
			length |= data[0] << 8;
		}
		bio_free_pages(bio);
		bio_put(bio);

		compressed = SQUASHFS_COMPRESSED(length);
		length = SQUASHFS_COMPRESSED_SIZE(length);
		index += 2;

		TRACE("Block @ 0x%llx, %scompressed size %d\n", index - 2,
		      compressed ? "" : "un", length);
	}
	if (length < 0 || length > output->length ||
			(index + length) > msblk->bytes_used) {
		res = -EIO;
		goto out;
	}

	if (next_index)
		*next_index = index + length;

	res = squashfs_bio_read(sb, index + extra_offset, length, &bio, &offset);
	if (res)
		goto out;

	if (compressed) {
		if (!msblk->stream) {
			res = -EIO;
			goto out_free_bio;
		}
#ifdef CONFIG_RTOS_EXTEND_SQUASHFS
		bio->bi_private = NULL;
#endif
		res = msblk->thread_ops->decompress(msblk, bio, offset, length, output);
#ifdef CONFIG_RTOS_SQUASHFS_BCJ
		if (res > 0 && bcj_index != BCJ_NONE)
			bcj_process_page_actor(output, res, bcj_index);
#endif
	} else {
		res = copy_bio_to_actor(bio, output, offset, length);
	}

out_free_bio:
	bio_free_pages(bio);
	bio_put(bio);
out:
	if (res < 0) {
		ERROR("Failed to read block 0x%llx: %d\n", index, res);
		if (msblk->panic_on_errors)
			panic("squashfs read failed");
	}
	return res;
}

